using UnityEngine;
using System.Collections;


public class Enemy : MonoBehaviour
{
    #region Attributs
    private Detection _viewDetection;
    private int _minNumPassage = 1;
    private Const.ENNEMY_TYPE _type;
    private EnemyMove _enemyMove;
    
    
    #endregion

    #region Properties
    public Detection ViewDetection
    {
        get { return _viewDetection; }
    }
    public int MinNumPassage
    {
        get { return _minNumPassage; }
        set { _minNumPassage = value; }
    }
    public Const.ENNEMY_TYPE Type
    {
        get { return _type; }
        set { _type = value; }
    }
    #endregion

    #region Methods
    void Awake()
    {
        _enemyMove = this.GetComponent<EnemyMove>();
    }

    // Use this for initialization
	void Start () {
        var detectionComponent = GetComponent<Detection>();
        if (null != detectionComponent)
            _viewDetection = detectionComponent;
	}
	
	// Update is called once per frame
	void Update () 
    {
    }

    public void Restart()
    {
        _enemyMove.UpdateDirection();
        _enemyMove.Restart();
        _enemyMove.UpdateTexture();
    }

    #endregion

    #region SAVE / LOAD

    public virtual void Save(XMLOutStream stream)
    {
        stream.Start("Enemy")
            .Content("numPassage", _minNumPassage)
            .Content("type", (int)_type)
            .Content("pos", _enemyMove.startPosition);            

        foreach (Vector2 v in _enemyMove.path)
        {
            stream.Content("path", v);
        }
        stream.End();
    }

    public virtual void Load(XMLInStream stream)
    {
        int tempType = 0;
        stream.Content("numPassage", out _minNumPassage)
            .Content("type", out tempType)
            .Content("pos", out _enemyMove.startPosition);
        _type = (Const.ENNEMY_TYPE)tempType;
        stream.List("path", delegate(XMLInStream stream2)
        {
            Vector2 path;
            stream2.Content(out path);
            _enemyMove.path.Add(path);
        });

         
    }

    #endregion SAVE / LOAD
}
